package com.zfl.thread.day04;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureDemo08 {

    private static int sum=0;
    public static void main(String[] args){
        ExecutorService pool = Executors.newFixedThreadPool(2);
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(()->{
            for (int i = 0; i < 100; i++) {
                sum+=i;
            }
            System.out.println(Thread.currentThread().getName()+" sum:"+sum);
            return sum;
        },pool).whenComplete((v,e)->{
            if (e==null) {
                System.out.println(Thread.currentThread().getName()+" v:"+v);
            }
        }).exceptionally(e->{
            System.out.println(e);
            return null;
        });
//        System.out.println(Thread.currentThread().getName()+" "+  completableFuture.get());
        System.out.println(Thread.currentThread().getName()+"正在执行");
        System.out.println(completableFuture.join());//不需要抛出异常
        pool.shutdown();


    }
}
